import {
  dispatchPage,
  costInsertOrUpdate,
  costApply,
  costDel,
  applyPay,
  urgentCost,
} from '../services/dispatch';
import React from 'react';
import { Input, InputNumber, message, Radio, Select } from 'antd';
import { DriverSelect } from '../components/Selection/DriverSelect';

// 表格列信息
const columns = [
  {
    title: '车牌号',
    dataIndex: 'carNo',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '司机姓名',
    dataIndex: 'driver',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '司机电话',
    dataIndex: 'drPhone',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '费用状态',
    dataIndex: 'status',
    width: '120px',
    render: text => {
      switch (text) {
        case 'ENTRY':
          return <span>生成</span>;
        case 'APPLY':
          return <span>申请</span>;
        case 'PASS':
          return <span>通过</span>;
        case 'APPLY_PAY':
          return <span>申请支付</span>;
        case 'URGENT_PAY':
          return <span>紧急支付</span>;
        case 'PAY':
          return <span>支付</span>;
        case 'PAY_ALLOW':
          return <span>可支付</span>;
        case 'PAY_FINISH':
          return <span>支付完成</span>;
        case 'CANCEL':
          return <span>作废</span>;
        default:
          return <span />;
      }
    },
  },
  {
    title: '支付时间',
    dataIndex: 'applyPayTime',
    width: '180px'
  },
  {
    title: '总费用',
    dataIndex: 'feeSum',
    width: '120px',
  },
  {
    title: '费用平均',
    dataIndex: 'avgFlag',
    width: '100px',
    editable: true,
    render: text => {
      switch (text) {
        case 'W':
          return <span>重量</span>;
        case 'V':
          return <span>体积</span>;
        case 'Z':
          return <span>整车</span>;
        default:
          return <span />;
      }
    },
  },
  {
    title: '提货费',
    dataIndex: 'pickFee',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '干线费',
    dataIndex: 'arteryFee',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '送货费',
    dataIndex: 'dropoffFee',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '加点费',
    dataIndex: 'workoverFee',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '装货费',
    dataIndex: 'loadupFee',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '卸货费',
    dataIndex: 'loadoffFee',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '包车费',
    dataIndex: 'carFee',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '燃油补助费',
    dataIndex: 'fuelFee',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '加班费',
    dataIndex: 'callbackFee',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '押车费',
    dataIndex: 'escortinFee',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '小费',
    dataIndex: 'tipFee',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '短驳费',
    dataIndex: 'shuttleFee',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '司机身份证',
    dataIndex: 'drIdcard',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '账户姓名',
    dataIndex: 'accountName',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '银行卡号',
    dataIndex: 'bankAccount',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
];

// 搜索框信息
const searchFields = [
  {
    cols: [
      {
        md: 4,
        label: '司机姓名',
        name: 'driver',
        component: <Input placeholder="请输入司机姓名" />,
      },
      {
        md: 4,
        label: '账户姓名',
        name: 'accountName',
        component: <Input placeholder="请输入账户姓名" />,
      },
      {
        md: 4,
        label: 'load号',
        name: 'loadNo',
        component: <Input placeholder="请输入load号" />,
      },
      {
        md: 4,
        label: '状态',
        name: 'status',
        component: (
          <Select placeholder="请输入订单状态">
            <Option value="ENTRY">生成</Option>
            <Option value="APPLY">申请</Option>
            <Option value="PASS">通过</Option>
            <Option value="APPLY_PAY">支付申请</Option>
            <Option value="PAY">支付</Option>
            <Option value="PAY_FINISH">支付完成</Option>
            <Option value="CANCEL">作废</Option>
          </Select>
        ),
      },
      {
        md: 4,
        label: '装货地',
        name: 'loadingPlace',
        component: <Input placeholder="请输入装货地" />,
      },
      {
        md: 4,
        label: '卸货地',
        name: 'loadoffPlace',
        component: <Input placeholder="请输入卸货地" />,
      },
    ],
  },
];

//子表格列表信息
const columnsChlidren = [
  {
    title: 'load号',
    dataIndex: 'loadNo',
    width: '100px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '装货工厂',
    dataIndex: 'loadingFactory',
    width: '100px',
  },
  {
    title: '装货地',
    dataIndex: 'loadingPlace',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '卸货工厂',
    dataIndex: 'loadoffFactory',
    width: '100px',
  },
  {
    title: '卸货地',
    dataIndex: 'loadoffPlace',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '订单状态',
    dataIndex: 'status',
    width: '80px',
    editable: false,
    ellipsis: true,
    render: text => {
      switch (text) {
        case 'ENTRY':
          return <span>生成</span>;
        case 'SPLIT':
          return <span>拆单</span>;
        case 'SPLITED':
          return <span>拆单完成</span>
        case 'DISPATCH':
          return <span>调度</span>;
        case 'COST_CONFIRMATION':
          return <span>成本确认</span>;
        case 'COST_AUDIT':
          return <span>成本审核</span>;
        case 'ORDER_AUDIT':
          return <span>订单审核</span>;
        case 'LOAD_IN':
          return <span>货物装车</span>;
        case 'LOAD_OFF':
          return <span>货物送达</span>;
        case 'RECEIPT':
          return <span>收到回单</span>;
        case 'COMMON':
          return <span>承运</span>;
        case 'DELIVERY':
          return <span>送达</span>;
        case 'GATHERING':
          return <span>待收款</span>;
        case 'GATHERING_AUDIT':
          return <span>收入审核</span>;
        case 'INVOICE':
          return <span>开票(足额)</span>;
        case 'INVOICE_HALF':
          return <span>开票(足额)</span>;
        case 'FINISH':
          return <span>完成</span>;
        default:
          return <span />;
      }
    },
  },
  {
    title: '业务类型',
    dataIndex: 'busType',
    width: '120px',
    editable: true,
    ellipsis: true,
    render: text => {
      switch (text) {
        case 'LD':
          return <span>零担</span>;
        case 'DC':
          return <span>单程</span>;
        case 'WF':
          return <span>往返</span>;
        default:
          return <span />;
      }
    },
  },
  {
    title: '要车类型',
    dataIndex: 'carType',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '提货日期',
    dataIndex: 'pickDate',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '到货日期',
    dataIndex: 'aogDate',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '数量',
    dataIndex: 'amount',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '体积',
    dataIndex: 'volume',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '重量',
    dataIndex: 'weight',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '备注',
    dataIndex: 'remark',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
];

// 子搜索框字段
const searchFieldsChlidren = [
  {
    cols: [
      {
        md: 6,
        label: '线路',
        name: 'line',
        component: <Input placeholder="请输入线路" />,
      },
      {
        md: 6,
        label: 'LOAD号',
        name: 'loadNo',
        component: <Input placeholder="请输入LOAD号" />,
      },
      {
        md: 6,
        label: '订单状态',
        name: 'status',
        component: (
          <Select placeholder="请输入订单状态">
            <Option value="SPLIT">拆单</Option>
            <Option value="DISPATCH">调度</Option>
          </Select>
        ),
      },
    ],
  },
];
// 申请费用表单列
const moneyRows = [
  {
    cols: [
      {
        label: '费用平均方式',
        name: 'avgFlag',
        md: 16,
        rules: [{ required: true, message: '必选' }],
        component: (
          <Radio.Group>
            <Radio.Button value="V">体积</Radio.Button>
            <Radio.Button value="W">重量</Radio.Button>
            <Radio.Button value="Z">整车</Radio.Button>
          </Radio.Group>
        ),
      },
    ],
  },
  {
    cols: [
      {
        label: '实际要车类型',
        name: 'realCarType',
        md: 8,
        component: (
          <Input placeholder="请输入实际要车类型" />
        ),
      },
    ],
  },
  {
    cols: [
      {
        label: '提货费',
        name: 'pickFee',
        md: 6,
        rules: [{ required: true, message: ' ' }],
        component: <InputNumber placeholder="请输入提货费" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '干线费',
        name: 'arteryFee',
        md: 6,
        rules: [{ required: true, message: ' ' }],
        component: <InputNumber placeholder="请输入干线费" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '送货费',
        name: 'dropoffFee',
        md: 6,
        rules: [{ required: true, message: ' ' }],
        component: <InputNumber placeholder="请输入送货费" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '加点费',
        name: 'workoverFee',
        md: 6,
        rules: [{ required: true, message: ' ' }],
        component: <InputNumber placeholder="请输入加点费" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '装货费',
        name: 'loadupFee',
        md: 6,
        rules: [{ required: true, message: ' ' }],
        component: <InputNumber placeholder="请输入账号" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '卸货费',
        name: 'loadoffFee',
        md: 6,
        rules: [{ required: true, message: ' ' }],
        component: <InputNumber placeholder="请输入卸货费" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '包车费',
        name: 'carFee',
        md: 6,
        rules: [{ required: true, message: ' ' }],
        component: <InputNumber placeholder="请输入包车费" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '燃油补助费',
        name: 'fuelFee',
        md: 6,
        rules: [{ required: true, message: ' ' }],
        component: <InputNumber placeholder="请输入燃油补助费" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '加班费',
        name: 'callbackFee',
        md: 6,
        rules: [{ required: true, message: ' ' }],
        component: <InputNumber placeholder="请输入加班费" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '押车费',
        name: 'escortinFee',
        md: 6,
        rules: [{ required: true, message: ' ' }],
        component: <InputNumber placeholder="请输入押车费" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '小费',
        name: 'tipFee',
        md: 6,
        rules: [{ required: true, message: ' ' }],
        component: <InputNumber placeholder="请输入小费" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '短驳费',
        name: 'shuttleFee',
        md: 6,
        rules: [{ required: true, message: ' ' }],
        component: <InputNumber placeholder="请输入短驳费" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '司机姓名',
        name: 'driver',
        md: 8,
        rules: [{ required: true, message: ' ' }],
        component: <DriverSelect />,
      },
    ],
  },
  {
    cols: [
      {
        label: '车牌号',
        name: 'carNo',
        md: 8,
        rules: [{ required: true, message: ' ' }],
        component: <Input placeholder="请输入车牌号" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '司机电话',
        name: 'drPhone',
        md: 8,
        rules: [{ required: true, message: ' ' }],
        component: <Input placeholder="请输入司机电话" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '司机身份证',
        name: 'drIdcard',
        md: 8,
        rules: [{ required: true, message: ' ' }],
        component: <Input placeholder="请输入司机身份证" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '账户姓名',
        name: 'accountName',
        md: 8,
        rules: [{ required: true, message: ' ' }],
        component: <Input placeholder="请输入账户姓名" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '银行卡号',
        name: 'bankAccount',
        md: 8,
        rules: [{ required: true, message: ' ' }],
        component: <Input placeholder="请输入银行卡号" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '实际体积',
        name: 'realVoPrice',
        md: 8,
        component:  <InputNumber placeholder="请输入实际体积" />,
      },
    ],
  },
  {
    cols: [
      {
        label: '实际重量',
        name: 'realWePrice',
        md: 8,
        component:  <InputNumber placeholder="请输入实际重量" />,
      },
    ],
  },
];
const requiredCols = {};

export default {
  namespace: 'dispatchOrder',

  state: {
    columns: columns, // 费用表头
    searchFields: searchFields, //搜索框信息
    searchFieldsChlidren: searchFieldsChlidren, // 搜索框信息
    searchValues: {}, // 搜索框里的值
    pageList: [], // 表格数据
    childrenTable: [], // 子表格数据
    pagination: {
      pageSize: 10,
      current: 1,
      size: 'small',
      showSizeChanger: true,
      showQuickJumper: true,
    }, // 表格分布数据
    columnsChlidren: columnsChlidren, // 表格列信息
    editId: '', // 编辑行id
    selectionIds: [], // 表格选择ID
    selectedRows: [], // 表格选择行
    selectionOrderIds: [], // ORDER表格选择ID
    requiredCols: requiredCols,
    orderModal: false, // 绑定订单选择框
    moneyModal: false, // 申请费用弹窗
    modalForm: {}, // 申请费用表单数据
    moneyRows: moneyRows, // 是请费用表单行
  },

  effects: {
    // 获取分页数据
    *page({ payload }, { call, put }) {
      const res = yield call(dispatchPage, payload);
      yield put({
        type: 'page_r',
        payload: res,
      });
    },
    *save({ payload }, { call, put }) {
      const res = yield call(costInsertOrUpdate, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    },
    *apply({ payload }, { call, put }) {
      const res = yield call(costApply, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    },

    *urgentCost({ payload }, { call, put }) {
      const res = yield call(urgentCost, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    },
    *applyPay({ payload }, { call, put }) {
      const res = yield call(applyPay, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    },
    *del({ payload }, { call, put }) {
      const res = yield call(costDel, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    },
  },
  reducers: {
    handleAdd(state) {
      debugger;
      const { pageList, pagination } = state;
      let addList = pageList.filter(page => {
        return page.id === 0;
      });
      if (addList.length) {
        message.warning('请提交后再添加！');
        return {
          ...state,
        };
      } else {
        pageList.unshift({ id: 0 });
        const newPagination = {
          pages: pagination.pages,
          pageSize: pagination.pageSize,
          total: pagination.total + 1,
          current: pagination.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pageList: pageList,
          pagination: newPagination,
        };
      }
    },
    // 处理编辑
    handleEdit(state, action) {
      return {
        ...state,
        editId: action.payload,
      };
    },
    // 处理弹窗
    handleOrderModal(state, action) {
      const { orderModal } = state;
      return {
        ...state,
        orderModal: !orderModal,
      };
    },

    // 费用表单值绑定
    handleModalChange(state, action) {
      const { modalForm } = state;
      const { fields } = action.payload;
      const newModalForm = {
        ...modalForm,
        ...fields,
      };
      return {
        ...state,
        modalForm: newModalForm,
      };
    },

    // 处理申请费用
    handleMoneyModal(state, action) {
      const { moneyModal, modalForm } = action.payload;
      return {
        ...state,
        moneyModal: moneyModal,
        modalForm: modalForm,
      };
    },
    // 表格选中Id
    handleSelection(state, action) {
      const { selectedRowKeys, selectedRows } = action.payload;
      return {
        ...state,
        selectionIds: selectedRowKeys,
        selectedRows: selectedRows,
      };
    },
    // 绑定搜索框数据
    handleSearchChange(state, action) {
      const { searchValues } = state;
      const newSearchForm = {
        ...searchValues,
        ...action.payload,
      };
      return {
        ...state,
        searchValues: newSearchForm,
      };
    },
    // 分页数据请求回来后处理
    page_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        const list = payload.result.records;
        const pagination = {
          pages: payload.result.pages,
          pageSize: payload.result.size,
          total: payload.result.total,
          current: payload.result.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pagination: pagination,
          pageList: list,
          editId: '',
          selectionIds: [],
          selectedRows: [],
        };
      } else {
        return {
          ...state,
        };
      }
    },
    // 处理保存
    save_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('保存成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '保存失败！');
      }
      return {
        ...state,
      };
    },
  },
};
